//
//  ANProductImageScrollViewCell.m
//  Ants
//
//  Created by ZHM on 16/3/28.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANProductImageScrollViewCell.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"

@interface ANProductImageScrollViewCell ()<MJPhotoBrowserDelegate>

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (weak, nonatomic) IBOutlet UIScrollView *imageViewScrollView;

//引用计时器属性
@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic, strong) ANBDProduct *product;

@end

@implementation ANProductImageScrollViewCell


#pragma mark - init Method
- (void)awakeFromNib {
    [super awakeFromNib];
    self.imageViewScrollView.pagingEnabled = YES;
    self.imageViewScrollView.showsHorizontalScrollIndicator = NO;
    self.imageViewScrollView.showsVerticalScrollIndicator = NO;
    //[self.contentView addBottomBorderWithColor:UIColorFromRGB(0xE2E1E6) width:0.5];
}
- (void)configureCell:(ANBDProduct *)product {
    self.product = product;
    [product.icons enumerateObjectsUsingBlock:^(NSDictionary *oneImageDic, NSUInteger idx, BOOL * _Nonnull stop) {

        NSString *url = oneImageDic[@"minPic"];
        UIImageView *oneProductImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        oneProductImageView.contentMode = UIViewContentModeScaleAspectFit;
        oneProductImageView.tag = idx;
        //开启图片交互
        oneProductImageView.userInteractionEnabled = YES;
        //添加手势操作
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        [oneProductImageView addGestureRecognizer:tap];

        CGRect frame = CGRectMake(ScreenRect.size.width * idx, 0, ScreenRect.size.width, 214);
        oneProductImageView.frame = CGRectInset(frame, 1, 0);
        UIImage *placeImage = [UIImage imageWithColor:UIColorFromRGB(0xE7EBEE)];
        [oneProductImageView sd_setImageWithURL:url.toURL
                               placeholderImage:placeImage
                                        options:SDWebImageRetryFailed|SDWebImageProgressiveDownload];
        
        
        [self.imageViewScrollView addSubview:oneProductImageView];
    }];
    
    //self.imageViewScrollView.alwaysBounceHorizontal = NO;
    [self.imageViewScrollView setContentSize:CGSizeMake(ScreenRect.size.width * self.product.icons.count, 0)];
    //总页数
    self.pageControl.numberOfPages = self.product.icons.count;
    //设置默认当前页
    self.pageControl.currentPage = 0;
    //设置定时器
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
}
#pragma mark-- 点击图片时候调用
- (void)tap:(UITapGestureRecognizer *)tap {
    [self stopScrollImage];
    UIImageView *tapView = (UIImageView *) tap.view;
    NSMutableArray *phs = [NSMutableArray array];
    int i = 0;
    for (NSDictionary *dic in _product.icons) {
        MJPhoto *ph = [[MJPhoto alloc] init];
        ph.url = [NSURL URLWithString:dic[@"maxPic"]];
        ph.index = i;
        ph.srcImageView = tapView;
        [phs addObject:ph];
        i++;
    }
    //创建浏览器
    MJPhotoBrowser *browser = [[MJPhotoBrowser alloc] init];
    browser.delegate = self;
    browser.photos = phs;
    browser.currentPhotoIndex = tapView.tag;
    [browser show];
}
- (void)end {
    [self startScrollImage];
}
- (void)scrollImage{
    CGPoint pit = self.imageViewScrollView.contentOffset;
    pit.x += self.imageViewScrollView.frame.size.width;
    if (pit.x == self.imageViewScrollView.contentSize.width) {
        pit.x = 0;
        [self.imageViewScrollView setContentOffset:pit animated:NO];
    }else{
        [self.imageViewScrollView setContentOffset:pit animated:YES];
    }
}

- (void)startScrollImage {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    //获取当前消息循环对象
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)stopScrollImage {
    //计时器停止 下次不能再用 必须重新创建新的
    [self.timer invalidate];
    self.timer = nil;
}


//即将开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self stopScrollImage];
}
//拖拽完毕
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self startScrollImage];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint point  = scrollView.contentOffset;
    self.pageControl.currentPage = point.x/self.imageViewScrollView.frame.size.width;

}
+ (instancetype)cellWithTableView:(UITableView *)tabalView {
    ANProductImageScrollViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"ANProductImageScrollViewCell" owner:nil options:nil] firstObject]; ;
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
